package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbResource;
import com.oig.sys.auth.vo.ResourceVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbResourceDao extends JpaRepository<TbResource, Integer> {

	TbResource getTbResourceById(Integer id);

	TbResource getByResourceCode(String resourceCode);

	@Query("select r.id from TbResource r where r.systemId=:systemId")
	List<Integer> findAllIdBySystemId(@Param("systemId") Integer systemId);

	@Query("select max(r.sort) from TbResource r where r.systemId=:systemId and r.parentId=:parentId")
	Integer findMaxSortByParentId(@Param("systemId") Integer systemId, @Param("parentId") Integer parentId);

	@Query("select new com.oig.sys.auth.vo.ResourceVo(r.id, r.systemId, r.resourceCode, r.resourceName,r.sort," +
			"r.icoClass,r.parentId,r.pathUrl, r.uiEvent, r.noDelFlag, r.status, r.remark," +
			"r.queryType,r.queryConditionCfg, r.querySortCfg,r.apiUrl, r.jsonText, r.isSupportTab, r.tabCfgJson, r.isShow, r.isQueryOnly," +
			"r.isPublicData,r.level, r.createUserName, r.createTime, r.lastUpdateName, r.lastUpdateTime)" +
			" from TbResource r " +
			" where r.id in(:idList)  order by r.sort")
	List<ResourceVo> findSystemMenuByIdList(@Param("idList") List<Integer> idList);


	@Query("select new com.oig.sys.auth.vo.ResourceVo(r.id, r.systemId, r.resourceCode, r.resourceName,r.sort," +
			"r.icoClass,r.parentId,r.pathUrl, r.uiEvent, r.noDelFlag, r.status, r.remark," +
			"r.queryType,r.queryConditionCfg, r.querySortCfg,r.apiUrl, r.jsonText, r.isSupportTab, r.tabCfgJson, r.isShow, r.isQueryOnly," +
			"r.isPublicData,r.level, r.createUserName, r.createTime, r.lastUpdateName, r.lastUpdateTime)" +
			" from TbResource r " +
			" where r.systemId=:systemId order by r.sort")
	List<ResourceVo> findSystemMenu(@Param("systemId") Integer systemId);

	@Query("select new com.oig.sys.auth.vo.ResourceVo(r.id, r.systemId, r.resourceCode, r.resourceName,r.sort," +
			"r.icoClass,r.parentId,r.pathUrl, r.uiEvent, r.noDelFlag, r.status, r.remark," +
			"r.queryType,r.queryConditionCfg, r.querySortCfg,r.apiUrl, r.jsonText, r.isSupportTab, r.tabCfgJson, r.isShow, r.isQueryOnly," +
			"r.isPublicData,r.level, r.createUserName, r.createTime, r.lastUpdateName, r.lastUpdateTime)" +
			" from TbResource r " +
			" where r.systemId=:systemId and r.status=:status  order by r.sort")
	List<ResourceVo> findSystemMenuByStatus(@Param("systemId") Integer systemId, @Param("status")String status);

}
